﻿using System.Windows.Forms;
using Main.Structure;
using Main.Substructure;
using System;

namespace Main.Menus.Submenus
{
    /// <summary>
    /// This Form is used for editing clients.
    /// </summary>
    public partial class ManageClient : Form
    {
        // Local variables:
        private Client client; // Shadow-copy of the Client value so we can use it after the constructor.

        /// <summary>
        /// The class constuctor.
        /// Takes the Client value passed to it and loads the form with it's data.
        /// </summary>
        /// <param name="client">The client the user wishes to edit.</param>
        public ManageClient(Client client)
        {
            this.client = client; // Creates the shadow-copy of the Client value.
            Text = "Managing " + client.Name + " | ID: " + client.AccountID; // Changes the window title.
            InitializeComponent();
            // Does the rest of what's needed to do before the form is loaded.
            Main();
        }

        /// <summary>This method contains all of the code we want to run before the form is loaded.</summary>
        private void Main()
        {
            // First we load all of the client's information into the form.
            #region Load Form Data
            txtBoxFirstName.Text = client.FirstName;
            txtBoxLastName.Text = client.LastName;
            txtBoxStreet.Text = client.Street;
            txtBoxCity.Text = client.City;
            txtBoxState.Text = client.State;
            txtBoxZip.Text = client.Zip;
            txtBoxPhone.Text = client.Phone;
            txtBoxNotes.Text = client.Notes;
            #endregion
        }

        /// <summary>
        /// This method is called whenever the "Cancel" button is clicked.
        /// This method closes and disposes of this form.
        /// </summary>
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Dispose();
        }

        /// <summary>
        /// This method is called whenever the "Modify Client" button is clicked.
        /// This method asks the user if they want to modify the client and if so, does it.
        /// </summary>
        private void btnModifyClient_Click(object sender, System.EventArgs e)
        {
            // First we want to make sure that they really want to modify the data and aren't just making a mistake.
            DialogResult result = Utilities.Message("Are you sure that you want to modify this client data?", "Modify Client Data?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            // If they really want to modify it, then go ahead and run a method that will.
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                if (!ModifyClientData())
                    Utilities.Message("Client's information could not be modified!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    Utilities.Message("Client's information was modified and saved.", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        /// <summary>This method edits the client's data with the form data and then saves it.</summary>
        private bool ModifyClientData()
        {
            // Wrap with a try/catch for error-checking.
            try
            {
                client.FirstName = txtBoxFirstName.Text;
                client.LastName = txtBoxLastName.Text;
                client.Street = txtBoxStreet.Text;
                client.City = txtBoxCity.Text;
                client.State = txtBoxState.Text;
                client.Zip = txtBoxZip.Text;
                client.Phone = txtBoxPhone.Text;
                client.Notes = txtBoxNotes.Text;

                client.EditAll();

                // If no errors occured, return true so we know that everything went okay.
                return true;
            }
            catch (Exception) { return false; }
        }
    }
}
